﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
using Pyco.Framework;
using System.IO;
using System.Text.RegularExpressions;
namespace AutoSendMail
{
    class Utility
    {
        static public bool IsValidEmailFormat(string strIn)
        {
            if (String.IsNullOrEmpty(strIn))
                return false;

            // Return true if strIn is in valid e-mail format.
            return Regex.IsMatch(strIn,
                   @"^(?("")(""[^""]+?""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
                   @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9]{2,17}))$",
                   RegexOptions.IgnoreCase);
        }


        static public bool IsValidEmailAddress(String emailAdd)
        {
            HttpWebRequest request;
            HttpWebResponse response;
            CookieContainer cookies;
            ServicePointManager.Expect100Continue = false;
            string url = string.Format("http://verify-email.org/");
            request = (HttpWebRequest)WebRequest.Create(url);

            //set header
            // request.Headers.Add("Host", "aloxovn.com");
            //request.Headers.Add("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.18) Gecko/20110614 Firefox/3.6.18");
            request.ContentType = "application/x-www-form-urlencoded";
            request.Method = "POST";



            //request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            //request.Accept = "Mozilla/5.0 (Windows NT 5.1; rv:10.0) Gecko/20100101 Firefox/10.0";
            //request.TransferEncoding = "gzip, deflate";

            //request.


            request.AllowAutoRedirect = true;
            request.CookieContainer = new CookieContainer();


            //set parameters
            string formParams = string.Format("check={0}",
                emailAdd
                );
            byte[] bytes = Encoding.ASCII.GetBytes(formParams);
            request.ContentLength = bytes.Length;
            using (Stream os = request.GetRequestStream())
            {
                os.Write(bytes, 0, bytes.Length);
            }

            response = (HttpWebResponse)request.GetResponse();

            Stream s = response.GetResponseStream();


            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            doc.Load(s);
            String xPath = "/html/body/div[3]/div[1]/p[10]";
            String resultText = doc.DocumentNode.SelectSingleNode(xPath).InnerText;
            return !(resultText.Contains("Result: Bad"));
        }
    }
}
